package com.xiyoukt.springboot.util;

import com.sun.xml.internal.org.jvnet.mimepull.MIMEMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * Description: springboot
 * ********************************************************
 * *                                                      *
 * *                     xiyoukt.com                      *
 * *          西游课堂 - Java程序猿的在线学习课堂          *
 * *                                                      *
 * ********************************************************
 * Created by xiyoukt on 2018/9/23 0:20
 */
@Component
public class MailUtil {

    @Value("${mail.from}")
    private String from;

    @Autowired
    private TemplateEngine templateEngine;

    @Autowired
    private JavaMailSender mailSender;

    private Logger log = LoggerFactory.getLogger(MailUtil.class);

    // 发送一封简单的邮件
    public void sendSimpleMail(String to, String subject, String text){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        try{
            mailSender.send(message);
            log.info("send mail success");
        }catch (Exception e){
            log.error("send mail error", e);
            // 邮件发送失败后，需要做相应的重试机制
        }
    }

    // html格式邮件
    public void sendHtmlMail(String to, String subject, String text){
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text, true);
            mailSender.send(mimeMessage);
            log.info("send mail success");
        } catch (MessagingException e) {
            e.printStackTrace();
            log.error("send mail error", e);
            // 邮件发送失败后，需要做相应的重试机制
        }
    }

    // 带附件
    public void sendAttachmentMail(String to, String subject, String text ,String path){
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text);

            FileSystemResource fileSystemResource = new FileSystemResource(new File(path));
            String fileName = path.substring(path.lastIndexOf(File.separator));
            helper.addAttachment(fileName, fileSystemResource);

            mailSender.send(mimeMessage);
            log.info("send mail success");
        } catch (MessagingException e) {
            e.printStackTrace();
            log.error("send mail error", e);
            // 邮件发送失败后，需要做相应的重试机制
        }
    }

    // 模板邮件
    public void sendTemplateMail(String to, String subject, String username){
        Context context = new Context();
        context.setVariable("username", username);
        String mailContext = templateEngine.process("mailTemplate", context);

        sendHtmlMail(to, subject, mailContext);
    }

}
